<?php
namespace app\flash\controller;
use think\Controller;
use QL\QueryList;
use think\Db;
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: *");
header('Access-Control-Allow-Methods: POST,GET','OPTIONS');
class Bourse extends Controller{
    public function bourse(){
        $url = 'https://www.bishijie.com/exchanges';
        $rulers = [
            'bourse_logo' => ['.cur_logo>img','src'],
            'bourse_name' => ['.cur_info_box>h2>a','text'],
            'bourse_business' => ['td:eq(1)>span','text'] ,
            'bourse_type'   => ['td:eq(2)>.cur_info_s','html'],
            'bourse_text'           => ['.jytit','text'],
            'bourse_support' => ['td:eq(3)>.jyinfo','html']
        ];
        $range = '#exchange_table>#table_body>tr';
        $data = QueryList::Query($url,$rulers,$range)->data;
        //var_dump($data);die;
        foreach ($data as $key => $value) {
            $type = $value['bourse_type'];
            $type = str_replace("<i>","",$type);
            $type = str_replace("</i>",",",$type);
            $type = trim($type,',');
            $value['bourse_type'] = $type;
            $res = Db::table('bourse')->where(['bourse_name'=>$value['bourse_name']])->find();
            if (empty($res)) {
                $insert = Db::table('bourse')->insert($value);
                if (empty($insert)) {
                    var_dump('采集入库成功');
                }else{
                    var_dump('采集入库失败');
                }
            }else{
                $insert = Db::table('bourse')->where(['bourse_name'=>$value['bourse_name']])->update($value);
                if ($insert==1) {
                    var_dump('采集更新成功');
                }
            }
        }
    }
    /**
     * 功能：交易所的查询
     * 时间：2019/08/14
     * 作者：张仕虎
     */
    public function findbourse(){
        $data = Db::table('bourse')->select();
        foreach ($data as $key => $value) {
            unset($data[$key]['bourse_support']);
            unset($data[$key]['bourse_text']);
        }
        if ($data) {
            return json(msg(1,$data,'查询成功'));
        }else{
            return json(msg(0,'','查询失败'));
        }
    }
    /**
     * 功能：币种和交易所的搜索
     * 时间：2019/08/26
     * 作者：张仕虎
     */
    public function boursecurrency(){
        $search = trim(input('search'));
        $where[] = ['symbol','like',"%$search%"];
        $user_id = trim(input('user_id'));
        $currency = Db::table('currency')
            ->where($where)
            ->field('id,symbol,name,logo,code')
            ->select();
            foreach ($currency as $key => $value) {
               $currency[$key]['is_zixuan'] = 0;
            }
        if(!empty($user_id)){
            foreach ($currency as $key=>$val){
                $is_exist = Db::name('currency_hangqing_zixuan')->where(array('uid'=>$user_id,'hid'=>$val['id']))->find();
                if(!empty($is_exist)){
                    $currency[$key]['is_zixuan'] = 1;
                }else{
                    $currency[$key]['is_zixuan'] =0;
                }
            }
        }
        $wheres[] = ['bourse_name','like',"%$search%"];
        $bourse   = Db::table('bourse')->where($wheres)->field('bourse_name,bourse_logo')->select();
        return json(msg(1,['currency'=>$currency,'bourse'=>$bourse],'查询成功'));
    }
}